package com.oracle.zebrafreelytraveling.ui;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.TextInputEditText;
import android.util.Log;
import android.view.View;
import android.widget.Button;

import com.oracle.zebrafreelytraveling.R;
import com.oracle.zebrafreelytraveling.data.RequestResult.ResUser;
import com.oracle.zebrafreelytraveling.data.bean.User;
import com.oracle.zebrafreelytraveling.net.HttpManager;
import com.oracle.zebrafreelytraveling.utils.constant.Constant;
import com.oracle.zebrafreelytraveling.utils.ToastUtil;
import com.oracle.zebrafreelytraveling.utils.UserUtil;

import org.litepal.crud.DataSupport;

import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.annotations.NonNull;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;

/**
 * Created by yydcnjjw on 2017/7/29.
 */

public class LoginActivity extends BaseActitvity implements View.OnClickListener {

    private TextInputEditText mAccount;
    private TextInputEditText mPassword;
    private Button mLogin;
    private Button mLogup;

    private Disposable mDisposable;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        initView();
    }

    private void initView() {

        mAccount = (TextInputEditText) findViewById(R.id.account);
        mPassword = (TextInputEditText) findViewById(R.id.password);
        mLogin = (Button) findViewById(R.id.login);
        mLogup = (Button) findViewById(R.id.logup);

        mLogin.setOnClickListener(this);
        mLogup.setOnClickListener(this);
    }

    private void login() {
        mLogin.setClickable(false);
        String account = mAccount.getText().toString();
        String password = mPassword.getText().toString();
        HttpManager.getInstance()
                .getService(Constant.BASE_URL)
                .getResLogin(account, password)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<ResUser>() {
                    @Override
                    public void onSubscribe(@NonNull Disposable d) {
                        mDisposable = d;
                    }

                    @Override
                    public void onNext(@NonNull ResUser resUser) {
                        DataSupport.deleteAll(User.class);
                        if (resUser.getCode().equals(Constant.REQUEST_SUCCESS)) {
                            User user = resUser.getData();
                            UserUtil.setUserInfoFromSP(user);
                            UserUtil.setUserInfoLocalBySP(user);
                        } else {
                            ToastUtil.showToast(getString(R.string.info_false));
                            mLogin.setClickable(true);
                        }
                    }
                    @Override
                    public void onError(@NonNull Throwable e) {
                        Log.d("login", e.getMessage());
                        mLogin.setClickable(true);
                    }

                    @Override
                    public void onComplete() {
                        startActivity(new Intent(LoginActivity.this, MainActivity.class));
                        finish();
                    }
                });
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_login;
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.login:
                login();
                break;
            case R.id.logup:
                //跳转到注册页面
                LogupActivity.startLogupActivity(LoginActivity.this);
                break;
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (mDisposable != null && !mDisposable.isDisposed()) {
            mDisposable.dispose();
        }
    }
}
